//
//  GSMainViewViewController.m
//  AgileNYC
//
//  Created by German on 03.06.14.
//  Copyright (c) 2014 Softlegion. All rights reserved.
//

#import "SGSMainViewController.h"
#import "SGSModelsManager.h"
#import "SGSServerManager.h"
#import "SGSUtils.h"
#import "SGSAppDelegate.h"

#if NEW_DESIGN
    NSString * EventsControllerName = @"SGSEventsViewController";
    NSString * LoginControllerName = @"SGSLoginViewController";
#else
    NSString * EventsControllerName = @"GSEventsViewController";
    NSString * LoginControllerName = @"GSLoginViewController";
#endif


@interface SGSMainViewController ()

@property (retain, nonatomic) NSTimer * tmrLoadEvents;
@property (assign, nonatomic) NSInteger attemptCount;

@end

@implementation SGSMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
  
    
#if AUTHORIZATION_MANDATORY
    UIViewController * vc;
    
    if ([[GSModelsManager sharedModelsManager] checkUserLogin]) {
        
        vc = [self.storyboard instantiateViewControllerWithIdentifier:EventsControllerName];
        [self.navigationController setNavigationBarHidden:YES animated:NO];

    } else {
    
        vc = [self.storyboard instantiateViewControllerWithIdentifier:LoginControllerName];
        [self.navigationController setNavigationBarHidden:YES animated:NO];
        
    }
#else
    
    //vc = [self.storyboard instantiateViewControllerWithIdentifier:EventsControllerName];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    self.attemptCount = 0;
    [self startTimer];
    
#endif
    

    //[self.navigationController pushViewController:vc animated:YES];
    
    GSLog(GSLogEvent, @"----- GSMainViewController Did Load");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    GSLog(GSLogEvent, @"----- GSMainViewController Did Appear");
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    GSLog(GSLogEvent, @"----- GSMainViewController Will Disappear");
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - NSTimer


-(IBAction) startTimer {
    if ((!self.tmrLoadEvents) || (![self.tmrLoadEvents isValid])) {
        self.tmrLoadEvents = [NSTimer scheduledTimerWithTimeInterval:2.0f
                                                              target:self
                                                            selector:@selector(timerCallback:)
                                                            userInfo:nil
                                                             repeats:YES];

        GSLog(GSLogInfo, @"[GSMainViewController] Timer started");
    }
}

-(IBAction) stopTimer {
    if ([self.tmrLoadEvents isValid]) {
        [self.tmrLoadEvents invalidate];
    }
    
    self.tmrLoadEvents = nil;
    GSLog(GSLogInfo, @"[GSMainViewController] Timer stopped");
}

-(void) timerCallback:(NSTimer*) pTimer {
    SGSModelsManager * modelsManager =  [SGSModelsManager sharedModelsManager];
    
    if (([modelsManager.arrayOfEvents count]>0) || (self.attemptCount>15)) { //wait for events loaded
    
        [self stopTimer];

        UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:EventsControllerName];
        [self.navigationController setNavigationBarHidden:YES animated:NO];
        
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        self.attemptCount++;
    }
}

@end
